<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="反馈类型" prop="feedbackType">
        <el-select
          v-model="queryParams.feedbackType"
          placeholder="请选择反馈类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.SCHOOL_WEEKLY_FEEDBACK_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="反馈标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入反馈标题"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['school:classes:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="周计划类型" align="center" prop="weeklyType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SCHOOL_WEEKLY_DAILY_TYPE" :value="scope.row.weeklyType" />
        </template>
      </el-table-column>
      <el-table-column label="反馈类型" align="center" prop="feedbackType">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.SCHOOL_WEEKLY_FEEDBACK_TYPE" :value="scope.row.feedbackType" />
        </template>
      </el-table-column>
      <el-table-column label="反馈标题" align="center" prop="title" />
      <el-table-column label="反馈内容" align="center" prop="description" />
      <el-table-column label="附件" align="center" prop="fileUrls" :show-overflow-tooltip="false">
        <template #default="scope">
          <FileListView :file-list="scope.row.fileUrls" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="200px"
      />
      <el-table-column label="操作" align="center" fixed="right" width="120">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row)"
            v-hasPermi="['school:activity:update']"
          >
            修改
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['school:activity:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 表单弹窗：添加/修改 -->
  <feedbackFormDialog ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { WeeklyPlanApi, WeeklyPlanFeedbackVO } from '@/api/school/weeklyplan'
import feedbackFormDialog from './feedbackFormDialog.vue'
import { useRoute } from 'vue-router'
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
const route = useRoute() // 路由
const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  dailyId?: number // 通知ID（主表的关联字段）
  weeklyType?: string // 周计划类型（1：教师周计划，2：学生周计划）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  dailyId: undefined as unknown,
  weeklyType: undefined as unknown,
  feedbackType: undefined as any,
  title: undefined as any
})
const queryFormRef = ref()
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WeeklyPlanApi.getWeeklyPlanFeedbackPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, row?: any) => {
  if (!route.query.dailyId) {
    message.error('请选择一个通知')
    return
  }
  formRef.value.open(type, {
    id: route.query.dailyId,
    weeklyType: route.query.weeklyType,
    data: row
  })
}
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await WeeklyPlanApi.deleteWeeklyPlanFeedback(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => route.query,
  (val: any) => {
    if (!val) {
      return
    }
    queryParams.dailyId = route.query.dailyId
    queryParams.weeklyType = route.query.weeklyType
    queryParams.pageNo = 1
    handleQuery()
  },
  { immediate: true, deep: true }
)
</script>
